{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Group Chat with Tools\n",
    "\n",
    "This notebook explains how to set up a group chat where each agent has unique capabilities and is equipped with specialized tools to perform specific tasks.\n",
    "\n",
    "## Installation\n",
    "```bash\n",
    "pip install -U ag2[openai]\n",
    "```\n",
    "\n",
    "> **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:  \n",
    "> ```bash\n",
    "> pip install -U autogen[openai]\n",
    "> ```\n",
    "> or  \n",
    "> ```bash\n",
    "> pip install -U ag2[openai]\n",
    "> ```\n",
    "> as `autogen`, and `ag2` are aliases for the same PyPI package.  \n",
    "\n",
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "from autogen import (\n",
    "    ConversableAgent,\n",
    "    GroupChat,\n",
    "    GroupChatManager,\n",
    "    LLMConfig,\n",
    "    UserProxyAgent,\n",
    "    register_function,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Agent Configuration\n",
    "  \n",
    "The `GroupChat` will contain three agents:\n",
    "- `sales_agent` - Responsible for selling tickets.\n",
    "- `cancellation_agent` - Handles ticket cancellations.\n",
    "- `user_proxy` - Acts as an intermediary between the user and other agents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_config = LLMConfig.from_json(path=\"OAI_CONFIG_LIST\").where(model=\"gpt-5-nano\")\n",
    "\n",
    "sales_agent = ConversableAgent(\n",
    "    name=\"SalesAgent\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "cancellation_agent = ConversableAgent(\n",
    "    name=\"CanelationAgent\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "user_proxy = UserProxyAgent(\n",
    "    name=\"user_proxy\",\n",
    "    human_input_mode=\"ALWAYS\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tools Registration\n",
    "\n",
    "In AG2, tool usage follows two steps:\n",
    "- An agent suggests a tool to use (via its LLM).\n",
    "- Another agent executes the tool.\n",
    "\n",
    "We will define two tools:\n",
    "- `buy_airplane_ticket`: Suggested by `sales_agent` and executed by `user_proxy` after user verification.\n",
    "- `cancel_airplane_ticket`: Suggested by `cancellation_agent` and executed by `user_proxy` after user verification."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def buy_airplane_ticket(from_location: str, to_location: str, date: str) -> str:\n",
    "    ticket_number = random.randint(1000, 9999)\n",
    "    return f\"\"\"Your ticket from {from_location} to {to_location} on {date} has been booked.\n",
    "Your ticket number is {ticket_number}.\n",
    "Please keep this number for future reference.\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "register_function(\n",
    "    buy_airplane_ticket,\n",
    "    caller=sales_agent,\n",
    "    executor=user_proxy,\n",
    "    description=\"Buy an airplane ticket\",\n",
    ")\n",
    "\n",
    "\n",
    "def cancel_airplane_ticket(ticket_number: str) -> str:\n",
    "    return f\"Your ticket with ticket number {ticket_number} has been canceled\"\n",
    "\n",
    "\n",
    "register_function(\n",
    "    cancel_airplane_ticket,\n",
    "    caller=cancellation_agent,\n",
    "    executor=user_proxy,\n",
    "    description=\"Cancel an airplane ticket\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating and Initiating the Group Chat\n",
    "\n",
    "Now, let's create and start the `GroupChat` with the three agents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "groupchat = GroupChat(\n",
    "    agents=[user_proxy, cancellation_agent, sales_agent],\n",
    "    speaker_selection_method=\"auto\",\n",
    "    messages=[],\n",
    ")\n",
    "\n",
    "manager = GroupChatManager(\n",
    "    name=\"group_manager\",\n",
    "    groupchat=groupchat,\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "user_proxy.initiate_chat(\n",
    "    recipient=manager,\n",
    "    message=\"I need to buy a plane ticket from New York to Los Angeles on 12th of April 2025\",\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Group Chat with Tools",
   "tags": [
    "agents",
    "tools",
    "group",
    "chat",
    "users",
    "guides"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
